﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using FrameWork.Util.Extension;
using FrameWork.Web;
using FrameWork.Web.Attribute;
using MyDb.DoMain.Entity.DbEntity;

namespace Admin.Web.Controllers
{
    [HandlerAuthorize(false)]
    public class ClientController : AdminControllerBase
    {
        [HttpGet]
        public ActionResult GetMenuJson()
        {
            var data = new
            {
                //dataItems = this.GetDataItemList(),
                //organize = this.GetOrganizeList(),
                //role = this.GetRoleList(),
                //duty = this.GetDutyList(),
                //user = "",
                authorizeMenu = this.GetMenuList(),
                //authorizeButton = this.GetMenuButtonList(),
            };
            return Content(data.ToJson());
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        private object GetMenuList()
        {
            var roleIds = "1";
            return ToMenuJson(BaseDataService.GetModuleList(roleIds), 0);
        }

        /// <summary>
        /// 构造菜单的方法
        /// </summary>
        private string ToMenuJson(List<T_Module> data, int parentId)
        {
            var sbJson = new StringBuilder();
            sbJson.Append("[");
            var entitys = data.FindAll(t => t.ParentId == parentId).Where(t => t.IsDel == false && t.IsUsed).ToList();
            if (entitys.Count > 0)
            {
                foreach (var item in entitys)
                {
                    var strJson = item.ToJson();
                    strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.Id) + "");
                    sbJson.Append(strJson + ",");
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();
        }
    }
}